package ct.zxd.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.Hashtable;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ct.zxd.db.Dao;
import ct.zxd.db.OrderDao;
import net.sf.json.JSONObject;

public class HandleOrder extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		int orderid;

		try {
			String address=request.getParameter("address");
			address = java.net.URLDecoder.decode(address, "utf-8");
			String userid=request.getParameter("userid");
			userid = java.net.URLDecoder.decode(userid, "utf-8");
			
//			log("order user "+userid);
//			log("order address "+address);
			
//			String ordertype=request.getParameter("ordertype");
//			ordertype = java.net.URLDecoder.decode(ordertype, "utf-8");
			 
	        String orderList= request.getParameter("list");
	        orderList = java.net.URLDecoder.decode(orderList, "utf-8");
	        Hashtable<String,String> ordermap = new Hashtable<String,String>();
	        ordermap.put("order", orderList);
	        orderList = ordermap.toString();
	        
	       // log("order orderList "+orderList);
	       
	        
//	        String note= request.getParameter("note");
//	        if(note != null){
//	        	note = java.net.URLDecoder.decode(note, "utf-8");
//	        }
	      
	        String money= request.getParameter("money");
	        money = java.net.URLDecoder.decode(money, "utf-8");
	        float totalMoney = Float.parseFloat(money);
	 
	        String shopid= request.getParameter("shopid");
	        shopid = java.net.URLDecoder.decode(shopid, "utf-8");
	        
//	        log("order shopid "+shopid);log("order money "+money);
	        
	        long date = new Date().getTime();
	        
	        OrderDao orderDao = new OrderDao();
	        orderid = orderDao.insertOrder(date, totalMoney, orderList, address, shopid, "", userid);

		} catch (Exception e) {
			e.printStackTrace();
			return;
		}
		
		//如果成功，就反馈给小程序端一个json，发送‘ok’
		JSONObject jb = new JSONObject();
		jb.put("orderid", orderid);
		response.getWriter().write(jb.toString());
	}

}
